package group2assignone.ucm;

import group2assignone.dao.MyDAO;
import group2assignone.model.Flight;
import group2assignone.view.ListFlightsView;
import group2assignone.view.OurView;
import java.util.ArrayList;

/**
 * Use Case: Change UI to Search Flights<br>
 * 1. System tries to connect with database<br>
 * 2. System loads the flights<br>
 * 3. system creates new list flights view with the flight details
 * 4. system starts the new list flights view
 * 5. system exits the current view
 * 1a. - database connection fails<br>
 *     - system loads dummy data for the flights<br>
 *
 * @author group2
 */
public class UCListFlights implements UCController {

    private OurView view;
    private ArrayList<Flight> flightArray;
    private MyDAO coms;

    /**
     * The customer is not logged in, a guest customer is created.
     * @param view
     */
    public UCListFlights(OurView view) {
        this.view = view;
        coms = UCStartup.getInstance().getDAO();
    }

    /**
     */
    public void run() {
        //1. System tries to connect with database
         if (coms.isConnected()) {
                //2. System loads the relevant flights
                flightArray = coms.getFlights();
            } else {//1a. - database connection fails<br>
                    // - system loads dummy data for the flights
                flightArray = coms.getDummyFlights();
            }
        //3. system creates new list flights view with the flight details
        ListFlightsView gui = new ListFlightsView(flightArray);
        //4. system starts the new list flights view
        gui.go();
        //5. system exits the current view
        view.dispose();
    }
}
